C# 拼接字符串方式生成html文件 winform程序生成html表单

您所在的位置:网站首页 sql 查找字符串出现的位置是什么 C# 拼接字符串方式生成html文件 winform程序生成html表单

C# 拼接字符串方式生成html文件 winform程序生成html表单

2023-03-20 14:39| 来源: 网络整理| 查看: 265

        两年前大四 去实习前帮别人 做的一个东西

        一个很老的 C/S架构的库管软件 数据库使用的是一个本地部署的sqlserver

        总之要求就是sql查询拿到数据后,以拼贴字符串的形式 替换模板html中的占位符,这样一个html格式的单据就在本地出现了。

        为什么要这么做?html浏览器打开直接就打印了 。转换成pdf也方便。 

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WindowsFormsApplication2 { class PrintBill { string name = null; List list; public PrintBill(string name, List list)//通过构造函数传入客户名和商品列表 { this.name = name; this.list = list; } public void ToPrint() { int i=0; FileStream fs = new FileStream("../../出货单-" + name + "-" + DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒")+ ".html", FileMode.Create);//创建一个新的出货单 foreach (string str in System.IO.File.ReadAllLines("../../print.html", Encoding.Default)) //读取出货单模板 { string aline = str; aline.Replace("&form["+i+"]$",list[i]);//将占位符替换为输入的数据 i++; byte[] data = System.Text.Encoding.Default.GetBytes(aline); //获得字节数组 fs.Write(data, 0, data.Length); //开始写入 fs.Flush(); //清空缓冲区 } fs.Close();//关闭流 } } }

这是html部分 通过上面的循环操作把里面的的占位符 删掉就行了 自己注意 i 变量对应的是哪一项数据即可——下面是html部分

销售单 table.gridtable { font-family: verdana,arial,sans-serif; font-size:15px; color:#333333; border-width: 1px; border-color: #666666; border-collapse: collapse; } table.gridtable th { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #dedede; } table.gridtable td { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #ffffff; } 商品出货单 客户名: $form[0]$ 客户电话:$form[1]$ 日期:$form[2]$ ;$form[6]$ ;$form[7]$ ;$form[8]$ ;$form[9]$ ;$form[10]$ ;$form[11]$ ;$form[12]$ ;$form[13]$ ;$form[14]$ ;$form[15]$ 总计:$form[3]$ 备注:$form[4]$ ; ; 客户签字:$form[5]$



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3